package com.ruoyi.planthopper.utils;

import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;

/**
 * 类作用：根据坐标点画检测框
 *
 * @author Mr-Le
 */
@Slf4j
public class ImgDrawCircleTool {
    /**
     * 根据坐标点、标签集合画 检测框
     *
     * @param list      坐标点集合
     * @param tag       标签集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     * @Param num1 种类1的数量
     * @Param num2 种类2的数量
     */
    public String doDrawRectangle(List<int[]> list, List<String> tag, String inImgPath, Integer num1, Integer num2, Integer num3, Integer num4) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));

                if (list.size() == tag.size()) {
                    for (int i = 0; i < list.size(); i++) {
                        Color newColor = judgeColorByTag(tag.get(i));
                        g.setColor(newColor);
                        // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                        g.drawRect(list.get(i)[0], list.get(i)[1], list.get(i)[2] - list.get(i)[0], list.get(i)[3] - list.get(i)[1]);
                    }
                    // 增加图例，每种虫有多少头显示 替换
                    String numData1 = "ccfs:" + num1;
                    String numData2 = "glrc:" + num2;
                    String numData3 = "dlrc:" + num3;
                    String numData4 = "dcfs:" + num4;

                    // 增加图例，每种虫有多少头显示
                    g.setFont(new Font("Time New Roman", Font.BOLD, 64));
                    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

                    g.setColor(Color.RED);
                    g.drawString(numData1, image.getWidth() - 300, 80);
                    g.setColor(Color.BLUE);
                    g.drawString(numData2, image.getWidth() - 300, 160);
                    g.setColor(Color.green);
                    g.drawString(numData3, image.getWidth() - 300, 240);
                    g.setColor(Color.magenta);
                    g.drawString(numData4, image.getWidth() - 300, 320);

                    FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                    ImageIO.write(image, "jpeg", fileOutputStream);
                    fileOutputStream.close();

                } else {
                    log.info("tag和corner对应出错，检查算法端!");
                    return null;
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 无目标时的绘制
     *
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     *
     */
    public String doDrawEmpty(String inImgPath) throws FileNotFoundException {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (file.exists()) {
            log.info("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setStroke(new BasicStroke(5.0f));


                // 增加图例，每种虫有多少头显示 替换
                String numData1 = "ccfs:0";
                String numData2 = "glrc:0";
                String numData3 = "dlrc:0";
                String numData4 = "dcfs:0";

                // 增加图例，每种虫有多少头显示
                g.setFont(new Font("Time New Roman", Font.BOLD, 64));
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));

                g.setColor(Color.RED);
                g.drawString(numData1, image.getWidth() - 300, 80);
                g.setColor(Color.BLUE);
                g.drawString(numData2, image.getWidth() - 300, 160);
                g.setColor(Color.green);
                g.drawString(numData3, image.getWidth() - 300, 240);
                g.setColor(Color.magenta);
                g.drawString(numData4, image.getWidth() - 300, 320);

                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                ImageIO.write(image, "jpeg", fileOutputStream);
                fileOutputStream.close();


            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            log.info("文件不存在!");
            return null;
        }
        return resPath;
    }

    /**
     * 根据tag判断检测框颜色
     *
     * @param tag 标签
     * @return 返回颜色
     */
    public static Color judgeColorByTag(String tag) {
        switch (tag) {
            case "1":
                return Color.red;
            case "2":
                return Color.BLUE;
            case "3":
                return Color.green;
            case "4":
                return Color.magenta;
            default:
                return Color.ORANGE;
        }
    }

//    public static Color judgeColorByTag(String tag) {
//        switch (tag) {
//            case "echinus":
//                return Color.red;
//            case "holothurian":
//                return Color.GREEN;
//            case "starfish":
//                return Color.BLUE;
//            default:
//                return Color.ORANGE;
//        }
//    }

    /**
     * 添加标签：_Result 标签
     *
     * @param srcPath 字符串路径
     * @return 返回加标签之后的字符串
     */
    private static String addResult(String srcPath) {
        String res = "";
        String[] str = new String[2];
        /* \\. 转义字符，相当于 . ,分隔出.前后的内容 */
        str = srcPath.split("\\.");
        res = str[0] + "__Result." + str[1];
        return res;
    }

    /**
     * 根据坐标点集合画 检测框
     * @param list 坐标点集合
     * @param inImgPath 输入图片路径
     * @return 返回结果图路径
     */
   /* public String doDrawRectangle(List<int[]> list, String inImgPath) {
        String resPath = null;
        String s = StringUtils.substringAfter(inImgPath, "profile");

        File file = new File(inImgPath);

        if (list.isEmpty()) {
            return null;
        }
        if (file.exists()) {
            System.out.println("file exists!");
            // 原图加上 result 标签
            resPath = addResult(inImgPath);
            try {
                BufferedImage image = ImageIO.read(new File(inImgPath));
                Graphics graphics = image.getGraphics();
                Graphics2D g = (Graphics2D) graphics;
                g.setColor(Color.red);
                g.setStroke(new BasicStroke(7.0f));
                for (int[] ints : list) {
                    // 检测框 坐标点 转换 x1,y1;x2,y2----------> x,y,w,h
                    g.drawRect(ints[0], ints[1], ints[2] - ints[0], ints[3] - ints[1]);
                }
                FileOutputStream fileOutputStream = new FileOutputStream(resPath);
                ImageIO.write(image, "jpeg", fileOutputStream);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        } else {
            System.out.println("文件不存在");
            return null;
        }
        return resPath;
    }*/
}
